package com.app.micaihu.view.army.header

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.View.OnClickListener
import android.widget.LinearLayout
import androidx.fragment.app.FragmentActivity
import com.app.base.BaseMvpHeaderView
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.utils.StatServiceUtils
import com.app.micaihu.view.army.activity.ArmyDetailActivity
import com.app.micaihu.view.army.activity.ArmyLevelActivity
import com.app.micaihu.view.army.activity.ArmyNoticeActivity
import com.app.micaihu.view.army.adapter.ArmyTaskListAdapter
import com.app.micaihu.view.army.contract.ArmyJoinedContract
import com.app.micaihu.view.army.dialog.ArmyTaskDialog
import com.app.micaihu.view.army.presenter.ArmyJoinedPresenter
import com.app.micaihu.view.bean.ArmyGroupInfo
import com.app.micaihu.view.bean.ArmySign
import com.app.micaihu.view.bean.MissionX
import com.app.micaihu.view.bean.SignIn
import com.app.micaihu.view.main.topic.PostSendActivity
import com.app.micaihu.view.main.view.WrapContentLinearLayoutManager
import com.app.micaihu.view.user.userinfo.activity.WebActivity
import com.app.micaihu.view.utils.loadImage
import com.app.utils.biz.Biz
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.SizeUtils
import com.blankj.utilcode.util.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.army_join_yes_header.view.*

class JoinedArmyHeader(context: Context) : BaseMvpHeaderView<ArmyJoinedContract.View, ArmyJoinedPresenter>(context = context), ArmyJoinedContract.View {

    override fun getLayoutId() = R.layout.army_join_yes_header
    override fun getPresenter() = ArmyJoinedPresenter()

    private val armyTaskDialog: ArmyTaskDialog by lazy {
        ArmyTaskDialog()
    }
    private var armyGroupInfo: ArmyGroupInfo? = null
    private var missionList: List<MissionX>? = null
    var gid: String = ""
    var originator: String = "" // 创始人id
    private var isSign: Boolean = false
    private var isExpand = false
    private var armyNotice = ""

    private val armyTaskListAdapter: ArmyTaskListAdapter by lazy {
        val armyTaskListAdapter = ArmyTaskListAdapter()
        rvTask.layoutManager = WrapContentLinearLayoutManager(context)
        rvTask.adapter = armyTaskListAdapter
        armyTaskListAdapter
    }

    companion object {
        //1 出操;2 评论;3点赞;4 阅读资讯;5 发帖
        const val taskType1: Int = 1
        const val taskType2: Int = 2
        const val taskType3: Int = 3
        const val taskType4: Int = 4
        const val taskType5: Int = 5
    }

    override fun setData() {

    }

    override fun addListeners() {
        cardView.setOnClickListener(detailListener)
        tvName.setOnClickListener(detailListener)
        tvNumber.setOnClickListener(detailListener)
        llInfo.setOnClickListener(detailListener)
        tvArmyNotice.setOnClickListener {
            val intent = Intent(context, ArmyNoticeActivity::class.java)
            intent.putExtra(Config.Extra.desc, armyNotice)
            intent.putExtra(Config.Extra.id, gid)
            intent.putExtra(Config.Extra.toUid, originator)
            ActivityUtils.startActivity(intent)
        }
        tvClose.setOnClickListener {
            llArmyNotice.visibility = View.GONE
        }
        tvArmySign.setOnClickListener {
            StatServiceUtils.event(Config.EventBauduId.teamlist_sign, "军团_签到")
            if (isSign) {
                ToastUtils.showShort("已出操")
            } else {
                mPresenter?.armySignIn(gid)
            }
        }
        armyTaskListAdapter.addChildClickViewIds(R.id.tvTaskFinish)
        armyTaskListAdapter.setOnItemChildClickListener { adapter, view, position ->
            val item = armyTaskListAdapter.getItem(position)
            when (view.id) {
                R.id.tvTaskFinish -> {
                    if (item.taskCurrentProgress != item.taskAllProgress) {
                        if (item.type == taskType5) {
                            val intent = Intent(context, PostSendActivity::class.java)
                            val bundle = Bundle()
                            bundle.putString(Config.Extra.id, gid)
                            intent.putExtras(bundle)
                            ActivityUtils.startActivity(intent)
                        } else {
                            LiveEventBus.get(Config.EventBus.goHome).post(true)
                        }
                    }
                }
            }
        }
        tvTaskMore.setOnClickListener {
            //http://app.micaiying.com/app/task/army?uid=360490372496507186&night=0
//            val url = URIContant.H5_NAME + "/app/task/army?uid=" + Biz.instance.uid()
//            val intent = Intent(context, WebActivity::class.java)
//            intent.putExtra(Config.Extra.url, url)
//            context.startActivity(intent)
            val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
            if (isExpand) {
                tvTaskMore.text = "展开全部"
                tvTaskMore.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.icon_news_arrow_down, 0)
                params.height = SizeUtils.dp2px(200f)
            } else {
                tvTaskMore.text = "收起"
                tvTaskMore.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.icon_news_arrow_up, 0)
                params.height = LinearLayout.LayoutParams.WRAP_CONTENT
            }
            rvTask.layoutParams = params
            isExpand = !isExpand
        }
        tvUpgrade.setOnClickListener {
            val intent = Intent(context, ArmyLevelActivity::class.java)
            val bundle = Bundle()
            bundle.putString(Config.Extra.id, gid)
            bundle.putString(Config.Extra.toUid, Biz.instance.uid())
            intent.putExtras(bundle)
            ActivityUtils.startActivity(intent)
        }
        tvLevel.setOnClickListener {
            val intent = Intent(context, ArmyLevelActivity::class.java)
            val bundle = Bundle()
            bundle.putString(Config.Extra.id, gid)
            bundle.putString(Config.Extra.toUid, Biz.instance.uid())
            intent.putExtras(bundle)
            ActivityUtils.startActivity(intent)
        }
        ivArmyLevel.setOnClickListener {
            val intent = Intent(context, ArmyLevelActivity::class.java)
            val bundle = Bundle()
            bundle.putString(Config.Extra.id, gid)
            bundle.putString(Config.Extra.toUid, Biz.instance.uid())
            intent.putExtras(bundle)
            ActivityUtils.startActivity(intent)
        }
        tvArmyRank.setOnClickListener {
            val intent = Intent(context, WebActivity::class.java)
            intent.putExtra(Config.Extra.url, armyGroupInfo?.rankListUrl)
            ActivityUtils.startActivity(intent)
        }
    }

    private var detailListener: OnClickListener = OnClickListener {
        val intent = Intent(context, ArmyDetailActivity::class.java)
        val bundle = Bundle()
        bundle.putString(Config.Extra.id, gid)
        bundle.putString(Config.Extra.toUid, Biz.instance.uid())
        intent.putExtras(bundle)
        ActivityUtils.startActivity(intent)
    }

    @SuppressLint("SetTextI18n")
    fun setHeaderData(armyGroupInfo: ArmyGroupInfo?) {
        llLoading.visibility = View.GONE
        this.armyGroupInfo = armyGroupInfo
        this.armyGroupInfo?.let {
            gid = it.id
            originator = it.originator
            ivArmyIcon.loadImage(it.icon)
            tvWarZone.text = it.war_zone
            tvName.text = it.title
            tvNumber.text = it.id
            tvGold.text = it.gold
            tvMember.text = "${it.member}/${it.member_limit}"
            tvLevel.text = "${it.level} >"
            tvArmyNotice.text = it.notice
            if (it.notice.isNullOrEmpty()) {
                llArmyNotice.visibility = View.GONE
            } else {
                armyNotice = it.notice
            }
            if (it.rankListUrl.isEmpty()) {
                tvArmyRank.visibility = View.GONE
            } else {
                tvArmyRank.visibility = View.VISIBLE
            }
        }
    }

    fun setArmyUpgrade(upgrade: Boolean) {
        this.armyGroupInfo?.let {
            if (it.originator == Biz.instance.uid() && upgrade) {
                tvUpgrade.visibility = View.VISIBLE
            } else {
                tvUpgrade.visibility = View.GONE
            }
        }
    }

    fun setHeaderLine(show: Boolean) {
        if (show) {
            viewLine.visibility = View.VISIBLE
        } else {
            viewLine.visibility = View.GONE
        }
    }

    fun setSignData(sign: List<SignIn>?) {
        sign?.let {
            isSign = it[0].isFinish
            if (isSign) {
                tvArmySign.setBackgroundResource(R.mipmap.army_task_btn_on_bg)
            } else {
                tvArmySign.setBackgroundResource(R.mipmap.army_task_btn_off_bg)
            }
        }
    }

    fun setTaskData(mission: List<MissionX>?) {
        this.missionList = mission
        this.missionList?.let {
            armyTaskListAdapter.setNewInstance(it.toMutableList())
        }
    }

    override fun handleArmySignIn(result: BaseBean<ArmySign?>?) {
        if (result == null) {
            ToastUtils.showShort(context.getString(R.string.net_error))
        } else {
            if (result.isSuccess()) {
                result.data?.let {
                    isSign = true
                    tvArmySign.setBackgroundResource(R.mipmap.army_task_btn_on_bg)
                    val bundle = Bundle()
                    bundle.putInt(Config.Extra.joinDays, it.joinDays)
                    bundle.putInt(Config.Extra.gold, it.gold)
                    bundle.putString(Config.Extra.desc, it.desc)
                    armyTaskDialog.arguments = bundle
                    armyTaskDialog.show(ActivityUtils.getTopActivity() as FragmentActivity)
                }
            } else {
                ToastUtils.showShort(result.msg)
            }

        }
    }

}